﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.EventAggregators;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.CustomerManagement.Models;

namespace IntoNails.Modules.CustomerManagement.ViewModels
{
    public partial class CustomerWorkspaceViewModel
    {
        private void OnCreateNewCustomerEventSubscribed(Customer customer)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            CustomerDataContext dataContext = new CustomerDataContext(customer.Gender, false);

            this.ShowCustomerInteractionDialog.Raise(
                customer,
                dataContext,
                this.CreateNewCustomerCallback,
                () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true)
                );
        }

        private void CreateNewCustomerCallback(Customer customerCallback)
        {
            // TODO: check input
            this._customerRepository.Add(customerCallback);
            this._customerRepository.Save();

            // add new user to list
            this.CustomerCollection.Add(customerCallback);

            // unlock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
        }

        private void OnEditCustomerEventSubscribed(object parameter)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            if (this.SelectedCustomer == null)
            {
                MessageBoxContext context = new MessageBoxContext("Please select a Customer to edit first.",
                                                                  GlobalInfoCaches.AssemblyName, MessageBoxButtons.OK,
                                                                  MessageBoxIcons.Exclamation);
                this.ShowMessageBox.Raise(context,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }

            CustomerDataContext dataContext = new CustomerDataContext(this.SelectedCustomer.Gender, true);

            this.ShowCustomerInteractionDialog.Raise(
                this.SelectedCustomer,
                dataContext,
                this.EditCustomerCallback,
                () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));
        }

        private void EditCustomerCallback(Customer customerCallback)
        {
            if (customerCallback == null)
            {
                throw new InvalidOperationException("customerCallback");
            }

            customerCallback.UpdatedDate = DateTime.Now;
            
            // un-lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
            
            // TODO: check input
            this._customerRepository.Save();
        }

        private void OnDeleteCustomerEventSubscribed(MessageBoxContext context)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            if (this.SelectedCustomer == null)
            {
                MessageBoxContext warningMsgBoxContext = new MessageBoxContext(
                    "Please select a Customer to edit first.",
                    GlobalInfoCaches.AssemblyName, MessageBoxButtons.OK,
                    MessageBoxIcons.Exclamation);

                this.ShowMessageBox.Raise(warningMsgBoxContext,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }
            
            this.ShowMessageBox.Raise(context, this.MessageBoxSubmitCallback,
                                      () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));
        }

        private void MessageBoxSubmitCallback(MessageBoxContext confirmedDeleteMsgBoxCallback)
        {
            // update data-storage
            this._customerRepository.Remove(this.SelectedCustomer.UniqueCode);
            this._customerRepository.Save();

            // update UI
            this.CustomerCollection.Remove(this.SelectedCustomer);

            // un-lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
        }
    }
}
